package com.example.mytest.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.provider.CalendarContract;
import android.util.AttributeSet;
import android.view.View;


/**
 * @ClassName: SampleView
 * @Description: java类作用描述
 * @Author: zdl
 * @Date: 2021/9/29 9:01
 */
public class SampleView extends View {

    public static final int RECT_SIZE = 8;
    private Point mSelectedPoint = null;
    private Point[] mPoints = new Point[POINT_ARRAY_SIZE];
    public static final int POINT_ARRAY_SIZE = 7;
    public static final int C_START = 0;
    public static final int C_END = 1;
    public static final int C_CONTROL_1 = 2;
    public static final int C_CONTROL_2 = 3;
    Paint paint;
    Path path;

    public SampleView(Context context) {
        super(context);
        mPoints[C_START] = new Point(100, 100);
        mPoints[C_CONTROL_1] = new Point(150, 100);
        mPoints[C_CONTROL_2] = new Point(150, 200);
        mPoints[C_END] = new Point(200, 200);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        path = new Path();
        paint.setStrokeWidth(2);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.BLACK);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        int sc = canvas.saveLayer(srcRect, paint, Canvas.ALL_SAVE_FLAG);//创建一个新的layer

//        canvas.drawColor();





//         draw the cubic line
        path.moveTo(mPoints[C_START].x, mPoints[C_START].y);
        path.cubicTo(mPoints[C_CONTROL_1].x, mPoints[C_CONTROL_1].y,
                mPoints[C_CONTROL_2].x, mPoints[C_CONTROL_2].y,
                mPoints[C_END].x, mPoints[C_END].y);

        canvas.drawPath(path, paint);



        path.moveTo(300, 100);
        path.cubicTo(350, 100, 350, 200, 400, 200);
        canvas.drawLine(100, 100, 300, 100, paint);
        canvas.drawLine(200, 200, 400, 200, paint);

//        LinearGradient linearGradient = new LinearGradient(getWidth(),400,0,0,Color.RED,Color.GREEN, Shader.TileMode.CLAMP);
//        paint.setShader(linearGradient);
//        paint.setStyle(Paint.Style.FILL);
//        canvas.drawRect(100, 100, 400, 200, paint);
////        canvas.save();
//        paint.setStyle(Paint.Style.STROKE);
//        paint.setColor(Color.RED);

//        PorterDuffXfermode mode = new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP);
//        paint.setXfermode(mode);
//        paint.setColor(Color.RED);
//        paint.setStyle(Paint.Style.FILL);
//        canvas.drawRect(100, 100, 400, 200, paint);

//        paint.setXfermode(null); // 还原混合模式
//        canvas.restoreToCount(sc);  // 将这个layer绘制到canvas默认的layer
    }
    private RectF dstRect, srcRect;
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        int width = w <= h ? w : h;
        int centerX = w/2;
        int centerY = h/2;
        int quarterWidth = width /4;
        srcRect = new RectF(100, 100, 400, 200);

    }
}
